package com.example.jdcompetition.Service.cvsPro.jd;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author henrydai
 * @date 2021/6/19 16:09
 */
@Deprecated
@Service
@Slf4j
public class ToTrueDate {
    long yy = 100000000;
    long ybw = 1000000;
    long yw = 10000;
    long yb = 100;
    public long toTrueDate(long date,long dateAdd){
        if(dateAdd==0)
            return date;
        long yyyy=date/yy;
        long MM = date%yy/ybw;
        long dd = date%ybw/yw;
        long HHmm = date%yw;
        long dd_add = dateAdd/10000;
        long HHmm_add = dateAdd%10000;
        dd_add+=(HHmm+HHmm_add)/2400;
        HHmm = (HHmm+HHmm_add)%2400;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        try {
            int max = getDaysOfMonth(sdf.parse(String.valueOf(date)));
            if(dd+dd_add>max)
            {
                dd=(dd+dd_add)%max;
                if(MM==12)
                {
                    MM=1;
                    yyyy+=1;
                }else
                {
                    MM+=1;
                }
            }
            else
            {
                dd+=dd_add;
            }
            return yyyy*yy+MM*ybw+dd*yw+HHmm;
        } catch (ParseException e) {
            log.error("日期转换出错");
            e.printStackTrace();
            return -1;
        }
    }
    private  int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
}
